//
//  CallEditViewController.m
//  CallTelphone
//
//  Created by QFX on 2023/5/24.
//

#import "CallEditViewController.h"

@interface CallEditViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *fromTextField;
@property (weak, nonatomic) IBOutlet UITextField *timeTextField;
@property (weak, nonatomic) IBOutlet UITextField *dianTextField;

@end

@implementation CallEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    int totalSeconds = [self.model.totalSeconds intValue];
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    
    self.phoneTextField.text = self.model.phone;
    self.fromTextField.text =  self.model.address;
    self.dianTextField.text =  self.model.dian;
    
    if (hours > 0 && minutes > 0){
        self.timeTextField.text = [NSString stringWithFormat:@"%d小时%d分钟",hours,minutes];
        return;
    }else if (minutes > 0){
        self.timeTextField.text = [NSString stringWithFormat:@"%d分钟",minutes];
        return;
    }else{
        self.timeTextField.text = [NSString stringWithFormat:@"%d秒钟",seconds];
        return;
    }
}


- (IBAction)save:(id)sender {
    if (self.phoneTextField.text.length > 0){
        self.model.phone = self.phoneTextField.text;
    }
    
    if (self.fromTextField.text.length > 0){
        self.model.address = self.fromTextField.text;
    }
    
    if (self.dianTextField.text.length > 0){
        self.model.dian = self.dianTextField.text;
    }
    
    if (self.timeTextField.text.length > 0){
        self.model.totalSeconds = self.timeTextField.text;
    }
    [self.model updateToDB];
    [self.navigationController popToRootViewControllerAnimated:YES];
}



@end
